home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 4 / BBS in a Box - Macintosh - Volume IV (January 1992) (BBS in a Box).iso / Files / Prog / B-C / BIG ARRAY.PA < prev    next >
Encoding:
Text File  |  1987-06-27  |  3.4 KB  |  130 lines  |  [TEXT/MACA]

  1. PROGRAM Big_Array;
  2. {
  3.     A demonstration of large arrays.  Three arrays are dimensioned,
  4.     each about 100K.  Turbo Pascal v1.00A
  5. }
  6. USES    MemTypes,QuickDraw,OSIntf;
  7.  
  8. CONST
  9.     intLenght   = 2;    { For integer arrays. }
  10.  
  11. TYPE
  12.     big     = record
  13.                 base    : Ptr;
  14.                 i, j    : longint
  15.               end;
  16.     IntPtr  = ^integer;
  17.  
  18. VAR
  19.     A, B, Sum       : big;
  20.     i, j, seconds   : LONGINT;
  21.     thePtr          : IntPtr;
  22.     theRow, theCol,
  23.     theLines        : INTEGER;
  24.  
  25. PROCEDURE cleanUp; FORWARD;
  26.  
  27. PROCEDURE dimension( VAR theArray : big; k,l : LONGINT);
  28.  
  29. BEGIN   { dimension }
  30.     WITH theArray DO
  31.         BEGIN
  32.             base := NewPtr(k*l*intLenght);
  33.             i := k;
  34.             j := l
  35.         END { WITH theArray }
  36. END;    { of dimension }
  37.  
  38. FUNCTION Index(VAR theArray : big; k,l : LONGINT):Ptr;
  39. {
  40.     Kind of like N dimensional arrays in FORTRAN
  41. }
  42. BEGIN   { Index }
  43.     WITH theArray DO
  44.         BEGIN
  45.             IF (k > i) or (l > j) then  { Do a range test. }
  46.                 BEGIN
  47.                     WriteLn('Range Error');
  48.                     ReadLn;
  49.                     cleanUp;
  50.                     halt
  51.                 END { IF }
  52.             ELSE
  53.                 Index := Pointer(i * (k - 1)*intLenght + j * (l - 1)*intLenght
  54.                     + LONGINT(base))
  55.         END { WITH }
  56. END;    { of Index }
  57.  
  58. PROCEDURE cleanUp;
  59.  
  60. BEGIN
  61.     disposPtr(A.base);
  62.     disposPtr(B.base);
  63.     disposPtr(Sum.base);
  64. END;
  65.  
  66. PROCEDURE Plus( VAR Array1, Array2, SumArray: big);
  67.  
  68. VAR
  69.     RowIndex, ColIndex  : LONGINT;
  70.     Sum                 : INTEGER;
  71.  
  72. BEGIN   { Plus }
  73.     IF (Array1.i <> Array2.i) or (Array2.i <> SumArray.i)
  74.         or (Array1.j <> Array2.j) or (Array2.j <> SumArray.j) then
  75.         BEGIN
  76.             WriteLn( 'Arrays are not similar.');
  77.             ReadLn;
  78.             Halt
  79.         END { IF }
  80.     ELSE
  81.         FOR RowIndex := 1 to SumArray.i DO
  82.             FOR ColIndex := 1 to SumArray.j DO
  83.                 BEGIN
  84.                     thePtr := IntPtr(Index(Array1,RowIndex,ColIndex));
  85.                     Sum := thePtr^;
  86.                     thePtr := IntPtr(Index(Array2,RowIndex,ColIndex));
  87.                     Sum := Sum + thePtr^;
  88.                     thePtr := IntPtr(Index(SumArray,RowIndex,ColIndex));
  89.                     thePtr^ := Sum
  90.                 END { FOR }
  91. END;    { of Plus }
  92.  
  93. FUNCTION Randomize (range : INTEGER): INTEGER;
  94. { See Chernicoff V1 p 25 }
  95.  
  96. VAR 
  97.   rawResult : LONGINT;
  98.  
  99. BEGIN { Randomize }
  100.  
  101.   rawResult := Abs(Random);
  102.   Randomize := (rawResult * range ) div 32768
  103.   
  104.  
  105. END; { Randomize }
  106.  
  107. BEGIN   { Big_Array }
  108.     WriteLn('Be patient.');
  109.     dimension(A,255,255);
  110.     dimension(B,255,255);
  111.     dimension(Sum,255,255);
  112.     FOR i := 1 to 255 DO    { Initialize the arrays. }
  113.         FOR j := 1 to 255 DO
  114.             BEGIN
  115.                 thePtr := IntPtr(index(A,i,j));
  116.                 thePtr^ := i + j;
  117.                 thePtr := IntPtr(index(B,i,j));
  118.                 thePtr^ := 2
  119.             END;
  120.     Plus(A,B,Sum);
  121.     FOR theLines := 1 to 20 DO  { Just write some random samples as a test. }
  122.         BEGIN
  123.             theRow := Randomize(255);
  124.             theCol := Randomize(255);
  125.             thePtr := IntPtr(Index(Sum,theRow,theCol));
  126.             WriteLn('The sum at (',theRow,',',theCol,') is ',thePtr^)
  127.         END;    { FOR theLines }
  128.     cleanUp;
  129.     ReadLn
  130. END.    { of Big_Array }